package com.gin.no_gay_alliance.adpter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemSearchHistoryBinding;
import com.gin.no_gay_alliance.manager.history.SearchHistoryManager;

/**
 * 搜索历史适配器，内部已实现加载、添加、删除、清空方法，外部需要处理选中操作
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/14 16:04
 **/
public class SearchHistoryAdapter extends BaseBindingRecyclerViewAdapter<ItemSearchHistoryBinding, String> implements View.OnClickListener {
    /**
     * 保存搜索历史的key 用于加载列表，删除关键字
     */
    @NonNull
    private final String datastoreKey;
    @NonNull
    private final OnKeywordSelectedListener onKeywordSelectedListener;


    public SearchHistoryAdapter(@NonNull Context context, @NonNull String datastoreKey, @NonNull OnKeywordSelectedListener onKeywordSelectedListener) {
        super(context, SearchHistoryManager.getHistory(datastoreKey));
        this.datastoreKey = datastoreKey;
        this.onKeywordSelectedListener = onKeywordSelectedListener;
    }


    @Override
    protected void onBindViewHolder(ViewHolder<ItemSearchHistoryBinding> holder, @NonNull ItemSearchHistoryBinding b, int position, String item) {
        b.tvKeyword.setText(item);

        b.ivDelete.setTag(position);
        b.ivDelete.setOnClickListener(this);

        b.getRoot().setTag(item);
        b.getRoot().setOnClickListener(this);
    }

    /**
     * 清空历史
     */
    public void clear() {
        int size = data.size();
        data.clear();
        handler.post(() -> notifyItemRangeRemoved(0, size));
        save();
    }

    /**
     * 添加一个关键字
     *
     * @param keyword 关键字
     */
    public void add(String keyword) {
        int index = data.indexOf(keyword);
        if (index != -1) {
            // 之前存在，移动位置
            data.remove(index);
            data.add(0, keyword);
            handler.post(() -> notifyItemMoved(index, 0));
        } else {
            // 不存在，添加
            data.add(0, keyword);
            handler.post(() -> notifyItemInserted(0));
        }
        // 保存
        save();
    }

    @Override
    public void onClick(View v) {
        // 删除
        if (v.getId() == R.id.iv_delete) {
            // 移除这一项
            int index = (int) v.getTag();
            data.remove(index);
            handler.post(() -> notifyItemRemoved(index));
            // 保存
            save();
        }
        // 选中
        if (v.getId() == R.id.layout_parent) {
            String keyword = (String) v.getTag();
            add(keyword);
            onKeywordSelectedListener.onKeywordSelected(keyword);
        }
    }

    private void save() {
        SearchHistoryManager.setHistory(datastoreKey, data);
    }

    /**
     * 选中关键字时的回调
     */
    public interface OnKeywordSelectedListener {

        /**
         * 选中关键字时的回调
         *
         * @param keyword 被选中的关键字
         */
        void onKeywordSelected(String keyword);
    }
}
